package com.itheima.day07.myList.test2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(Integer.valueOf(111));
        list.add(Integer.valueOf(222));
        list.add(Integer.valueOf(333));
        System.out.println(list); // [111, 222, 333]

//        void add(int index,E e)       向集合中指定索引位置添加元素
        list.add(0, 999);
        System.out.println(list); // [999, 111, 222, 333]

//        E remove(int index)            删除指定索引处的元素并返回
        System.out.println(list.remove(3)); // 333

//        E set(int index,E e)              修改指定索引处的元素,返回被修改的元素
        System.out.println(list.set(0, 888)); // 999

//        E get(int index)                   获取指定索引处的元素
        System.out.println(list.get(0)); // 888


        // 普通for循环遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
        System.out.println("-------------");

        // 迭代器遍历
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
        System.out.println("-------------");

        /*while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
        System.out.println("-------------");*/

        // 增强for循环遍历
        for (Integer integer : list) {
            System.out.print(integer + " ");
        }
        System.out.println();
        System.out.println("-------------");
        System.out.println("-------------");

        Collection<Integer> collection = new ArrayList<>();
        collection.add(Integer.valueOf(111));
        collection.add(Integer.valueOf(222));
        collection.add(Integer.valueOf(333));

        for (Integer integer : collection) {
            System.out.print(integer + " ");
        }
        Iterator<Integer> itit = collection.iterator();
        while (itit.hasNext()) {
            System.out.print(itit.next() + " ");
        }


    }
}
